import * as api from "../api/api";
import request from "../utils/request";
import { pageSize } from "../utils/common";
export default {
  namespace: "crowd",
  state: {
    crowds: { totalCount: 0, items: [] },
    crowdSearchs: { pageIndex: 1, pageSize, keywords: undefined },
    crowdsCategories: { items: [] },
    crowdJoins: { totalCount: 0, items: [] },
    SerchCrowdJoins: { pageIndex: 1, pageSize, CrowdFundingId: 0 },
    crowdComments: { totalCount: 0, items: [] },
    crowdCommentSearchs: { pageIndex: 1, pageSize, CrowdFundingId: 0 },
    crowdDetail: {}
  },
  effects: {
    /**
     * 获取众筹项目列表
     * @param param0
     * @param param1
     */
    *getCrowds({ payload }, { call, put }) {
      const { pageIndex, pageSize } = payload;
      const formSerchKey = payload.keywords,
        formCategoryId = payload.categoryId,
        formCrowdfundingState = payload.crowdState,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const crowds = yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageGetAllCrowdFundingAsync(
          {
            formSerchKey,
            formCategoryId,
            formCrowdfundingState,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          crowds,
          crowdSearchs: {
            formSerchKey,
            formCategoryId,
            formCrowdfundingState,
            pageIndex,
            pageSize
          }
        }
      });
    },
    /**
     * 保存众筹项目
     * @param param0
     * @param param1
     */
    *saveCrowd({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageSaveCrowdFundingAsync(
          { form }
        )
      );
      const { crowdSearchs } = yield select(({ crowd }) => crowd);
      yield put({ type: "getCrowds", payload: { ...crowdSearchs } });
      callback && callback(true);
    },
    /**
     * 更改众筹项目状态开始
     * @param param0
     * @param param1
     */
    *StartCrowd({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageStartCrowdFundingStateAsync(
          { form }
        )
      );
      const { crowdSearchs } = yield select(({ crowd }) => crowd);
      yield put({ type: "getCrowds", payload: { ...crowdSearchs } });
      callback && callback(true);
    },
     /**
     * 更改众筹项目状态结束
     * @param param0
     * @param param1
     */
    *EndCrowd({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageEndCrowdFundingStateAsync(
          { form }
        )
      );
      const { crowdSearchs } = yield select(({ crowd }) => crowd);
      yield put({ type: "getCrowds", payload: { ...crowdSearchs } });
      callback && callback(true);
    },
    /**
     * 查询众筹参与人信息
     * @param param0
     * @param param1
     */
    *SerchCrowdJoins({ payload }, { call, put }) {
      const { pageIndex, pageSize, crowdFundingId } = payload;
      const formCrowdFundingId = crowdFundingId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const crowdJoins = yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageGetAllCrowdFundingPeoplesAsync(
          { formCrowdFundingId, formSkipCount, formMaxResultCount }
        )
      );
      yield put({
        type: "save",
        payload: {
          crowdJoins,
          SerchCrowdJoins: { crowdFundingId, pageIndex, pageSize }
        }
      });
    },
    /**
     * 查询众筹评论信息
     * @param param0 
     * @param param1 
     */
    *SerchCrowdComments({ payload }, { call, put }) {
      const { pageIndex, pageSize, crowdFundingId } = payload;
      const formCrowdFundingId = crowdFundingId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const crowdComments = yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageGetAllCrowdFundingCommentAsync(
          { formCrowdFundingId, formSkipCount, formMaxResultCount }
        )
      );
      yield put({
        type: "save",
        payload: {
          crowdComments,
          crowdCommentSearchs: { crowdFundingId, pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取众筹分类
     * @param param0
     * @param param1
     */
    *getCrowdCategories({ payload }, { call, put }) {
      const crowdsCategories = yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageGetAllCrowdFundingCategoryAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { crowdsCategories } });
    },
    /**
     * 保存众筹类别
     * @param param0
     * @param param1
     */
    *saveCrowdCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagecrowdFundingManageApiFetchParamCreactor.managecrowdFundingManageSaveCrowdFundingCategoryAsync(
          { form }
        )
      );
      yield put({ type: "getCrowdCategories" });
      callback && callback(true);
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, search }) => {
        switch (pathname) {
          case '/crowdfunding/crowdfundings':
            dispatch({ type: "getCrowds", payload: { pageIndex: 1, pageSize } });
            break;
        }
      });
    }
  }
};
